<template>
  <div class="app-container">
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wechat:article:edit']"
        >编辑</el-button>
      </el-col>
    </el-row> -->
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <!-- <el-form-item label="文章作者" prop="articleAuthor">
          <el-input v-model="form.articleAuthor" placeholder="请输入文章作者" />
      </el-form-item>-->
      <el-form-item label="文章标题">
        <el-input v-model="form.articleTitle" placeholder="请输入文章标题" disabled />
      </el-form-item>
      <!-- <el-form-item label="文章列表缩略图" prop="articleThumbUrl">
          <el-input v-model="form.articleThumbUrl" placeholder="请输入文章列表缩略图" />
      </el-form-item>-->
      <!-- <el-form-item label="文章类型">
          <el-select v-model="form.articleType" placeholder="请选择文章类型">
            <el-option
              v-for="dict in articleTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
      </el-form-item>-->
      <!-- <el-form-item label="文章内容" prop="articleContent">
          <el-input v-model="form.articleContent" placeholder="请输入文章内容" />
      </el-form-item>-->
      <el-form-item label="道路救援">
        <Editor v-model="form.articleContent" style="margin-bottom:40px" />
      </el-form-item>
      <!-- <el-form-item label="浏览人数" prop="viewNum">
          <el-input v-model="form.viewNum" placeholder="请输入浏览人数" />
        </el-form-item>
        <el-form-item label="点赞人数" prop="likeNum">
          <el-input v-model="form.likeNum" placeholder="请输入点赞人数" />
        </el-form-item>
        <el-form-item label="评论人数" prop="commentNum">
          <el-input v-model="form.commentNum" placeholder="请输入评论人数" />
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
      </el-form-item>-->
      <!-- <el-form-item label="文章状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
      </el-form-item>-->
    </el-form>
    <div  class="dialog-footer" style="text-align: center;">
      <el-button type="primary" @click="submitForm">保存</el-button>
      <!-- <el-button @click="cancel">取 消</el-button> -->
    </div>
  </div>
</template>

<style>
.editor {
  line-height: normal !important;
  height: 500px;
}
</style>
<script>
import {
  listArticle,
  getArticle,
  delArticle,
  addArticle,
  updateArticle,
  exportArticle
} from "@/api/wechat/article";
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";
export default {
  components: {
    Editor
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 展示文章表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 文章类型字典
      articleTypeOptions: [],
      // 文章状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articleAuthor: undefined,
        articleTitle: undefined,
        articleThumbUrl: undefined,
        articleType: undefined,
        articleContent: undefined,
        viewNum: undefined,
        likeNum: undefined,
        commentNum: undefined,
        orderNum: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        articleTitle: [
          { required: true, message: "文章标题不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    // this.getList();
    // this.getDicts("sys_article_type").then(response => {
    //   this.articleTypeOptions = response.data;
    // });
    // this.getDicts("sys_normal_disable").then(response => {
    //   this.statusOptions = response.data;
    // });
    this.getAssistance()
  },
  methods: {
    /** 查询展示文章列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询展示文章列表 */
    getAssistance() {
      this.loading = true;
      getArticle(2).then(response => {
        this.form = response.data;
      });
    },
    // 文章类型字典翻译
    articleTypeFormat(row, column) {
      return this.selectDictLabel(this.articleTypeOptions, row.articleType);
    },
    // 文章状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        articleId: undefined,
        articleAuthor: undefined,
        articleTitle: undefined,
        articleThumbUrl: undefined,
        articleType: undefined,
        articleContent: undefined,
        viewNum: undefined,
        likeNum: undefined,
        commentNum: undefined,
        delFlag: undefined,
        orderNum: undefined,
        status: "0",
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.articleId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加展示文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const articleId = row.articleId || this.ids;
      getArticle(articleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改展示文章";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.articleId != undefined) {
            updateArticle(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addArticle(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const articleIds = row.articleId || this.ids;
      this.$confirm(
        '是否确认删除展示文章编号为"' + articleIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delArticle(articleIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有展示文章数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportArticle(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>